package com.onpositive.gamemaker.spriteeditor;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class SpriteContentProvider implements ITreeContentProvider
{

	private final Map<String, SpriteCategory> descriptors;

	public SpriteContentProvider(Map<String, SpriteCategory> descriptors)
	{
		this.descriptors = descriptors;
	}

	@Override
	public void dispose()
	{
		//Do nothing
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public Object[] getElements(Object inputElement)
	{
		Set<String> keySet = descriptors.keySet();
		Object[] result = new Object[keySet.size()];
		int i = 0;
		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();)
		{
			String key = (String) iterator.next();
			result[i++] = descriptors.get(key);
		}
		Arrays.sort(result);
		return result;
	}

	@Override
	public Object[] getChildren(Object parentElement)
	{
		if (parentElement instanceof SpriteCategory)
		{
			return ((SpriteCategory) parentElement).getSpriteList();
		}
		return null;
	}

	@Override
	public Object getParent(Object element)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean hasChildren(Object element)
	{
		return (element instanceof SpriteCategory) && ((SpriteCategory) element).getSpriteCount() > 0;
	}

}
